package MyIOTest;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test02 {
    public static void main(String[] args) throws IOException {
        //姓氏网站
        String FamilyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        //男生名字网站
        String BoyNameNet = "https://www.mingziwang.cn/baobaoqiming/33951.html";
        //女生名字网站
        String GirlNameNet = "https://www.xingzuo.com/qiming/detail/235708.html";
        String FamilyNameTest = toWeb(FamilyNameNet);
        //System.out.println(FamilyNameTest);
        String BoyNameTest = toWeb(BoyNameNet);
        //System.out.println(BoyNameTest);
        String GirlNameTest = toWeb(GirlNameNet);
        //System.out.println(GirlNameTest);
        ArrayList<String> Family = getData(FamilyNameTest, "(.{4})(，|。)", 1);
        //System.out.println(Family);
        ArrayList<String> Boy = getData(BoyNameTest, "([\\u4E00-\\u9FA5]{2})(、)", 1);
//        System.out.println(Boy);
        ArrayList<String> Girl = getData(GirlNameTest, "([\\u4E00-\\u9FA5]{2})(、)", 1);
        //System.out.println(Girl);
        ArrayList<String> FamilyList = new ArrayList<>();
        ArrayList<String> BoyList = new ArrayList<>();
        ArrayList<String> GirlList = new ArrayList<>();
        for (String s : Family) {
            for (int i = 0; i < s.length(); i++) {
                FamilyList.add(String.valueOf(s.charAt(i)));
            }
        }
        for (String s : Boy) {
            if(!BoyList.contains(s)){
                BoyList.add(s);
            }
        }
        for (String s : Girl) {
            if(!GirlList.contains(s)){
                GirlList.add(s);
            }
        }
//        System.out.println(FamilyList);
//        System.out.println(BoyList);
//        System.out.println(GirlList);
        ArrayList<String> name = toName(FamilyList, BoyList, GirlList, 50, 50);
        Collections.shuffle(name);
        BufferedWriter bw = new BufferedWriter(new FileWriter("csb.txt"));
        for (String s : name) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();
        System.out.println(name);
    }
    public static ArrayList<String> toName(ArrayList<String> Family,ArrayList<String> Boy,ArrayList<String> Girl,int boyCount,int girlCount){
        ArrayList<String> list = new ArrayList<>();
        Random r = new Random();
        HashSet<String> Boys = new HashSet<>();
        while (true){
            if(boyCount == Boys.size()){
                break;
            }
            Collections.shuffle(Family);
            Collections.shuffle(Boy);
            Boys.add(Family.get(0) + Boy.get(0));
        }
        HashSet<String> Girls = new HashSet<>();
        while (true){
            if(Girls.size() == girlCount){
                break;
            }
            Collections.shuffle(Family);
            Collections.shuffle(Girl);
            Girls.add(Family.get(0) + Girl.get(0));
        }
        for (String boy : Boys) {
            list.add(boy + "-男-" + (r.nextInt(10) + 18));
        }
        for (String girl : Girls) {
            list.add(girl + "-女-" + (r.nextInt(10) + 18));
        }
        return list;
    }
    public static ArrayList<String> getData(String str,String regex,int index){
        ArrayList<String> list = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()){
            String group = matcher.group(index);
            list.add(group);
        }
        return list;
    }
    public static String toWeb(String web) throws IOException {
        //进入网站
        URL url = new URL(web);
        URLConnection ucc = url.openConnection();
        InputStreamReader isr = new InputStreamReader(ucc.getInputStream());
        int ch;
        StringBuilder sb = new StringBuilder();
        while ((ch = isr.read()) != -1){
            sb.append((char) ch);
        }
        isr.close();
        return sb.toString();
    }
}
